<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\home;

use app\common\controller\AuthCheck;

/**
 * 采购管理
 * @package app\erp\home
 */
class Purchase extends AuthCheck
{
    /**
     * 采购单列表
     * @author shiqiang
     * @return mixed
     */
    public function list(){
		//参数接收
        $params 		= $this->getParams(0);//0 get 1 post 
        $user_id    	= intval(isset($params["user_id"]) ? $params["user_id"] : 0);
		
		/*******搜索条件*********/
		$title 				= isset($params["title"]) ? $params["title"] : ''; 
		$start_time 		= isset($params["start_time"]) ? $params["start_time"] : ''; 						// 开始时间
		$end_time   		= isset($params["end_time"]) ? $params["end_time"] : date('Y-m-d H:i:s');			// 结束时间
		$supplier_id		= intval(isset($params["supplier_id"]) ? $params["supplier_id"] : 0);       		// 供应商id
		$procurement_method	= intval(isset($params["procurement_method"]) ? $params["procurement_method"] : 0); //采购方式
		$arrival_warehouse_id	= intval(isset($params["arrival_warehouse_id"]) ? $params["arrival_warehouse_id"] : 0);// 仓库id
		$handled_by			= isset($params["handled_by"]) ? $params["handled_by"] : ''; //  经办人
        $code   			= isset($params["code"]) ? $params["code"] : '';			 // 采购单号
        $status   			= isset($params["status"]) ? $params["status"] : '';			 // 状态
        /*******搜索条件*********/
		
        //数据验证
        $Validate = validate('Purchase', 'validate');
        if( !$Validate -> scene('purchase_list') -> check($params) ){
            json_out(-1001, $Validate -> getError(), []);//验证失败
        }

        //逻辑处理
        $list = controller('Purchaselogic', 'logic') -> list($user_id, $title, $supplier_id, $procurement_method, $arrival_warehouse_id, $handled_by, $code, $status);

        //数据返回
        json_out($list['code'], $list['message'], $list['data']);        
    }

    /**
     * 申请采购
     * @author shiqiang
     * @return mixed
     */
    public function add(){
		//参数接收
        $params = $this->getParams(1);//0 get 1 post 
        //测试参数
        /* $params['purchase_sku'] = [ //参数
            [
                'sku_id' => 16,
                'num' => 7,
                'price' => 17.5,
            ],
            [
                'sku_id' => 17,
                'num' => 2,
                'price' => 500.3,
            ],
        ]; */

        //测试参数

        $user_id           		= intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        $title            		= strval(isset($params["title"]) ? $params["title"] : '');
        $supplier_id			= intval(isset($params["supplier_id"]) ? $params["supplier_id"] : 0);
        $procurement_method		= intval(isset($params["procurement_method"]) ? $params["procurement_method"] : 0);
        $arrival_warehouse_id	= intval(isset($params["arrival_warehouse_id"]) ? $params["arrival_warehouse_id"] : 0);
        $advance_charge			= isset($params["advance_charge"]) ? $params["advance_charge"] : 0;
        $arrival_days			= intval(isset($params["arrival_days"]) ? $params["arrival_days"] : 0);
        $remark					= strval(isset($params["remark"]) ? $params["remark"] : '');
        $handled_by				= strval(isset($params["handled_by"]) ? $params["handled_by"] : '');
        $purchase_sku			= isset($params["purchase_sku"]) ? $params["purchase_sku"] : [];
        
        //数据验证
        $Validate = validate('Purchase', 'validate');
        if( !$Validate -> scene('purchase_add') -> check($params) ){
            json_out(-1001, $Validate -> getError());//验证失败
        }
        
        //逻辑处理
        $list = controller('Purchaselogic', 'logic') -> add($user_id, $title, $supplier_id, $procurement_method, $arrival_warehouse_id, $advance_charge, $arrival_days, $remark, $handled_by, $purchase_sku);

        //数据返回
        json_out($list['code'], $list['message']);        
    }
	
	 /**
     * 采购单详情
     * @author shiqiang
     * @return mixed
     */
    public function details(){
		//参数接收
        $params 		= $this->getParams(0);//0 get 1 post 
        $user_id    	= intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        $id    			= intval(isset($params["id"]) ? $params["id"] : 0);
		
        //数据验证
        $Validate = validate('Purchase', 'validate');
        if( !$Validate -> scene('purchase_details') -> check($params) ){
            json_out(-1001, $Validate -> getError(), []);//验证失败
        }

        //逻辑处理
        $list = controller('Purchaselogic', 'logic') -> details($user_id, $id);

        //数据返回
        json_out($list['code'], $list['message'], (object)$list['data']);        
    }
}

